from django.db import models
from taggit.managers import TaggableManager
from sorl.thumbnail import ImageField
from django.contrib.auth.models import User

# Create your models here.


class UserProfile(models.Model):
    #This field id required
    user = models.OneToOneField(User, unique=True)
    #Other fields
    voted_on_addon = models.ForeignKey("UserProfile", null=True,
                                       blank=True, related_name='+')
    voted_on_theme = models.ForeignKey('UserProfile', null=True, blank=True)
    website = models.CharField(max_length=255, blank=True)
    receive_email = models.BooleanField(default=False)
    receive_featured = models.BooleanField(default=False)
    biography = models.TextField(blank=True)
    twitter = models.CharField(max_length=255, blank=True)
    vimeo = models.CharField(max_length=255, blank=True)
    youtube = models.CharField(max_length=255, blank=True)
    facebook = models.CharField(max_length=255, blank=True)
    blenderartists = models.CharField(max_length=255, blank=True)

    def __unicode__(self):
        return self.user.username
User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])


class category(models.Model):
    category_name = models.CharField(max_length=255)
    category_description = models.TextField()

    def __unicode__(self):
        return self.category_name


class blenderversion(models.Model):
    version = models.CharField(max_length=5)

    def __unicode__(self):
        return self.version


class versionclass(models.Model):
    versiontype = models.CharField(max_length=20)

    def __unicode__(self):
        return self.versiontype


class addon(models.Model):
    try:
        user = User.objects.get()
    except:
        addon_image = ImageField(upload_to='userfiles/addon_images')
        addon_link = models.FileField(upload_to='userfiles/addon_files')
    else:
        addon_image = ImageField(upload_to='userfiles/' + str(user.id)
                              + '/' + '/addon_images')
        addon_link = models.FileField(upload_to='userfiles/'
                                  + str(user.id) + '/addon_files')
    addon_name = models.CharField(max_length=255)
    addon_description = models.TextField()
    addon_pub_date = models.DateTimeField('date published')
    addon_license = models.FileField(upload_to="files", blank=True)
    addon_category = models.ForeignKey(category)
    addon_vote_plus = models.IntegerField(default='0')
    addon_vote_minus = models.IntegerField(default='0')
    addon_is_gpl = models.BooleanField(default=True)
    addon_author = models.ForeignKey('auth.User', related_name='+')
    addon_visible = models.BooleanField(default=True)
    addon_tags = TaggableManager(blank=True)
    addon_downloads = models.IntegerField(default='0')
    addon_blender_version = models.ForeignKey(blenderversion)
    addon_voted_by = models.ManyToManyField(UserProfile, blank=True)
    addon_versiontype = models.ForeignKey(versionclass)

    def __unicode__(self):
        return self.addon_name


class theme(models.Model):
    try:
        user = User.objects.get()
    except:
        theme_image = ImageField(upload_to='userfiles/theme_images', blank=True)
        theme_link = models.FileField(upload_to='userfiles/theme_files', blank=True)
    else:
        theme_image = ImageField(upload_to='userfiles/' + str(user.id)
                             + '/theme_images', blank=True)
        theme_link = models.FileField(upload_to='userfiles/' + str(user.id)
                                  + '/theme_files', blank=True)
    theme_name = models.CharField(max_length=255)
    theme_description = models.TextField()
    theme_pub_date = models.DateTimeField('date published')
    theme_vote_plus = models.IntegerField(default='0')
    theme_vote_minus = models.IntegerField(default='0')
    theme_author = models.ForeignKey('auth.User', related_name='+')
    theme_visible = models.BooleanField(default=True)
    theme_tags = TaggableManager(blank=True)
    theme_downloads = models.IntegerField(default='0')
    theme_voted_by = models.ManyToManyField(UserProfile, blank=True)

    def __unicode__(self):
        return self.theme_name
